﻿// <copyright file="AnalyticsUnitTesting.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-12-27</date>
// <summary>AnalyticsUnitTesting class definition</summary>

namespace Microsoft.WebAnalytics.Testing
{
    using System;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using Microsoft.WebAnalytics;

    /// <summary>
    /// Unit Test utility class for Silverlight Analytics Framework
    /// </summary>
    public sealed class AnalyticsUnitTesting
    {
        /// <summary>
        /// Prevents a default instance of the AnalyticsUnitTesting class from being created.
        /// </summary>
        private AnalyticsUnitTesting()
        {
        }

        /// <summary>
        /// Create a test page for a web analytics service
        /// </summary>
        /// <param name="app">the Silverlight application</param>
        /// <param name="testPage">the test page returned from 
        /// <see cref="Microsoft.Silverlight.Testing.UnitTestSystem.CreateTestPage()"/></param>
        /// <returns>the root visual</returns>
        /// <remarks>Call this function from the <see cref="System.Windows.Application.Startup"/> event 
        /// handler and set <see cref="System.Windows.Application.RootVisual"/> to its return value.</remarks>
        /// <exception cref="ArgumentNullException">if app or testPage is null.</exception>
        public static UIElement CreateAnalyticsTestPage(Application app, UIElement testPage)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }

            if (testPage == null)
            {
                throw new ArgumentNullException("testPage");
            }

            var grid = new Grid();

            var service = app.ApplicationLifetimeObjects.OfType<WebAnalyticsService>().FirstOrDefault();

            System.Diagnostics.Debug.Assert(service != null, "There should be a WebAnalyticsService in the Application.ApplicationLifetimeObjects.");

            service.AttachServices(grid);

            grid.Children.Add(testPage);

            return grid;
        }
    }
}
